#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkReply>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowTitle("Http客户端");
    manager = new QNetworkAccessManager(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    //获取到输入框中的url
    QUrl url(ui->lineEdit->text());

    //2.构造请求
    QNetworkRequest request(url);

    //3.接收响应
    QNetworkReply* response = manager->get(request);
    //4.get方法是非阻塞等待响应，发过来的数据如何处理，是由response中的信号觉得
    connect(response,&QNetworkReply::finished,this,[=](){
        if(response->error() == QNetworkReply::NoError)
        {
            //正确响应了
            QString html = response->readAll();
            ui->plainTextEdit->setPlainText(html);
        }
        else
        {
            ui->plainTextEdit->setPlainText(response->errorString());
        }
        response->deleteLater();
    });
}
